// export default {
//   inserted: function (el, binding, vnode) {
//     let oDiv = el
//     let moveDiv = el
//     let patent = binding.modifiers.patent
//     if (patent) {
//       moveDiv = el.parentElement
//     }
//     oDiv.onmousedown = function (e) {
//       let l = e.clientX - moveDiv.offsetLeft
//       let t = e.clientY - moveDiv.offsetTop
//       document.onmousemove = function (e) {
//         moveDiv.style.left = e.clientX - l + 'px'
//         moveDiv.style.top = e.clientY - t + 'px'
//       }
//       oDiv.onmouseup = function () {
//         document.onmousemove = null
//         oDiv.onmouseup = null
//       }
//     }
//   }
//
// }
function inserted (el, binding, vNode) {
  // el.setAttribute('style', 'position: absolute; z-index: 9999')
}

function bind (el, bindding, vNode) {
  el.setAttribute('draggable', true)
  let left, top, width, height
  el._dragstart = function (event) {
    event.stopPropagation()
    left = event.clientX - el.offsetLeft
    top = event.clientY - el.offsetTop
    width = el.offsetWidth
    height = el.offsetHeight
  }
  el._dragEnd = function (event) {
    event.stopPropagation()
    left = event.clientX - left
    top = event.clientY - top
    el.style.left = left + 'px'
    el.style.top = top + 'px'
    el.style.width = width + 'px'
    el.style.height = height + 'px'
  }
  el._documentAllowDraop = function (event) {
    event.preventDefault()
  }
  document.body.addEventListener('dragover', el._documentAllowDraop)
  el.addEventListener('dragstart', el._dragstart)
  el.addEventListener('dragend', el._dragEnd)
  window.addEventListener('resize', el._checkPosition)
}

function unbind (el, bindding, vNode) {
  document.body.removeEventListener('dragover', el._documentAllowDraop)
  el.removeEventListener('dragstart', el._dragstart)
  el.removeEventListener('dragend', el._dragEnd)
  window.removeEventListener('resize', el._checkPosition)
  delete el._documentAllowDraop
  delete el._dragstart
  delete el._dragEnd
}

export default {
  bind,
  unbind,
  inserted
}
